//
//  SSEmoticonTools.swift
//  SSSina
//
//  Created by ss on 2017/7/10.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit
// 最大列数
private let SSEMOTICONMAXCOL = 7
// 最大行数
private let SSEMOTICONMAXROW = 3
// 最大个数
private let SSEMOTICONMAXCOUNT = (SSEMOTICONMAXCOL*SSEMOTICONMAXROW) - 1
class SSEmoticonTools: NSObject {
    
    // 全局访问点
    static let share: SSEmoticonTools = SSEmoticonTools()
    
    // 表情键盘bundle对象
    lazy var emoticonBundle: Bundle = {
        // 获取路径
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        let bundle = Bundle(path: path)!
        return bundle
    }()
    
    // 默认
    lazy var normalEmoticons:[SSEmoticonModel] = {
        return self.loadEmoticons(fileName: "default")
    }()
    
    // emoji
    lazy var emojiEmoticons:[SSEmoticonModel] = {
        return self.loadEmoticons(fileName: "emoji")
    }()
    
    // lxh
    lazy var lxhEmoticons:[SSEmoticonModel] = {
        return self.loadEmoticons(fileName: "lxh")
    }()
    
    func test(){
        
        
    }
    
}
//MARK: - 通过路径获取不同的模型一维数组
extension SSEmoticonTools{
    fileprivate func loadEmoticons(fileName: String) -> [SSEmoticonModel]{
        // 路径
        let file = emoticonBundle.path(forResource: "\(fileName)/info.plist", ofType: nil)!
        // 字典数组
        let plistArray = NSArray(contentsOfFile: file)!
        // 使用YYModel
        let tempList = NSArray.yy_modelArray(with: SSEmoticonModel.self, json: plistArray) as! [SSEmoticonModel]
        return tempList
    }
    // 通过一维数组获取二维数组
    /*
     默认: 108 [[20],[20],[20],[20],[20],[8]]
     emoji: 80 [[20],[20],[20],[20]]
     lxh : 40 [[20],[20]]
     
     lxh : 40 [[20-(0-19)],[20]-(20-39)]
     */
    func loadEmoticonGroups(emoticons:[SSEmoticonModel]) ->[[SSEmoticonModel]] {
        
        // 计算传入的一维数组应该显 示多少页
        let pageCount = (emoticons.count + (SSEMOTICONMAXCOUNT - 1))/SSEMOTICONMAXCOUNT
        
        // 创建一个临时可变的二位空数组
        var tempList:[[SSEmoticonModel]] = [[SSEmoticonModel]]()
        
        // 遍历count
        for i in 0..<pageCount{
            
            let location = SSEMOTICONMAXCOUNT * i
            var length = SSEMOTICONMAXCOUNT
            
            // 防止截取越界
            if location + length > emoticons.count {
                length = emoticons.count - location
            }
            
            // 计算范围
            let range = NSRange(location: location, length: length)
            
            let array = (emoticons as NSArray).subarray(with: range) as! [SSEmoticonModel]
            // 保存一维数组
            tempList.append(array)
        }
        return tempList
    }
}
